home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / cli / bangtools.lha / src / peek.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-16  |  2.9 KB  |  109 lines

  1. /* poke - read a value directly from memory
  2.  *
  3.  * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation.  This software is provided "as is" without express or
  10.  * implied warranty.
  11.  *
  12.  * V1.0: 27/Nov/94 first version
  13.  */
  14. #define THIS_PROGRAM    "peek"
  15. #define THIS_VERSION    "1.0"
  16.  
  17. #include <exec/types.h>
  18. #include <exec/libraries.h>
  19. #include <dos/dos.h>
  20. #include <dos/rdargs.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #define SysBase_DECLARED
  25. #include <proto/exec.h>
  26. #include <proto/dos.h>
  27. #include <proto/utility.h>
  28.  
  29. extern struct Library *SysBase;
  30.  
  31. static const char amiga_version[] = "\0$VER: " THIS_PROGRAM " " THIS_VERSION " (" __COMMODORE_DATE__ ")";
  32.  
  33. const char Template[] = "ADDR/A,B=BYTE/S,S=SHORT=W=WORD/S,L=LONG/S,H=X=HEX/S";
  34. __aligned struct {
  35.     STRPTR  addr;
  36.     LONG    isbyte, isword, islong;
  37.     LONG    hex;
  38. } Args;
  39.  
  40.  
  41. void
  42. _main()
  43. {
  44.     struct RDArgs *rdargs;
  45.     APTR addr;
  46.     ULONG val;
  47.     LONG rc = RETURN_OK;
  48.  
  49.     /* RawDoFmt() 'put' function
  50.      * MC68000 machine code:
  51.      *      move.b d0,(a3)+
  52.      *      rts
  53.      * Idea by Doug Walker, walker@unx.sas.com
  54.      */
  55.     __aligned const unsigned char rawdocode[] = { 0x16, 0xC0, 0x4E, 0x75 };
  56.  
  57.  
  58.     if( SysBase->lib_Version < 37 ) {
  59.         #define MESSAGE "requires AmigaOS 2.04 or higher\n"
  60.         Write(Output(), MESSAGE, sizeof(MESSAGE)-1);
  61.         _exit(RETURN_FAIL);
  62.         #undef MESSAGE
  63.     }
  64.  
  65.     if( rdargs = ReadArgs(Template, (LONG *)&Args, NULL) ) {
  66.         int used = 0;
  67.         if( Args.isbyte )   used++;
  68.         if( Args.isword )   used++;
  69.         if( Args.islong )   used++;
  70.         if( used > 1 ) {
  71.             PutStr("only one type specifier allowed\n");
  72.             rc = RETURN_ERROR;
  73.         }
  74.         else {
  75.             char buf[16] = {0};
  76.             char *endp1;
  77.  
  78.             addr = (APTR)strtoul(Args.addr, &endp1, 0);
  79.             if( *endp1 != '\0' ) {
  80.                 PutStr("invalid address\n");
  81.                 rc = RETURN_ERROR;
  82.             }
  83.             else {
  84.                 if( Args.islong )
  85.                     val = *(ULONG *)addr;
  86.                 else
  87.                 if( Args.isword )
  88.                     val = (ULONG)(*(USHORT *)addr);
  89.                 else
  90.                     val = (ULONG)(*(UBYTE *)addr);
  91.  
  92.                 if( Args.hex )
  93.                     endp1 = "0x%lx\n";
  94.                 else
  95.                     endp1 = "%ld\n";
  96.                 RawDoFmt(endp1, (APTR)&val, (void (*)())rawdocode, buf);
  97.                 PutStr(buf);
  98.             }
  99.         }
  100.         FreeArgs(rdargs);
  101.     }
  102.     else {
  103.         PutStr("required argument missing\n");
  104.         rc = RETURN_ERROR;
  105.     }
  106.     _exit((int)rc);
  107. }
  108.  
  109.